package com.topisv.tms.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.entity.User;
import com.topisv.tms.entity.WorkGuaranteeTask;
import com.topisv.tms.entity.work.WorkBatch;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.repository.ShopSourceDao;
import com.topisv.tms.repository.UserDao;
import com.topisv.tms.repository.WorkGuaranteeTaskDao;
import com.topisv.tms.repository.work.WorkBatchDao;
import com.topisv.tms.repository.work.WorkOrderDao;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.base.BaseService;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly=true)
public class WorkGuaranteeTaskService extends BaseService{

	@Autowired
	private WorkGuaranteeTaskDao workGuaranteeTaskDao;
	@Autowired
	private WorkBatchDao workBatchDao;
	@Autowired
	private WorkOrderDao workOrderDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private ShopSourceDao shopSourceDao;
	
	@Transactional(readOnly=false)
	public void saveWorkGuaranteeTask(String batchCode){
		ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		User user=this.userDao.findOne(curUser.id);
		WorkBatch batch=this.workBatchDao.loadbatchByBatchCode(batchCode);
		String warehouseCode=batch.getWarehouseCode();
		String customerCode=batch.getCustomerCode();
		//查找MID要打保修单的店铺
		List<String> shopCodes = shopSourceDao.findByShopCodes();
		if(shopCodes.isEmpty()){
			return;
		}
		//筛选出这次批次里要打保修单的单
		List<WorkOrder> workOrders = workOrderDao.loadOrderByBatchCodeAndShopCode(batchCode, shopCodes);
		Map<String, List<WorkOrder>> map = new HashMap<String, List<WorkOrder>>();
		for(WorkOrder wo : workOrders){
			List<WorkOrder> wos = map.get(wo.getOrder().getShopCode());
			if(wos==null){
				wos = new ArrayList<WorkOrder>();
				map.put(wo.getOrder().getShopCode(), wos);
			}
			wos.add(wo);
			
		}
		for(Map.Entry<String, List<WorkOrder>> entry : map.entrySet()){
			WorkGuaranteeTask guaranteeTask = new WorkGuaranteeTask();
			guaranteeTask.setBatchCode(batchCode);
			guaranteeTask.setCustomerCode(customerCode);
			guaranteeTask.setWarehouseCode(warehouseCode);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			guaranteeTask.setCreateTime(sdf.format(new Date()));
			guaranteeTask.setCreaterId(user.getJobuuid().toString());
			guaranteeTask.setGuaranteeStatus("0");
			workGuaranteeTaskDao.save(guaranteeTask);
			DecimalFormat df=new DecimalFormat("000000");
			Long id=guaranteeTask.getId();
			String guaranteeCode="G"+customerCode+df.format(id);
			guaranteeTask.setTaskCode(guaranteeCode);
			for(WorkOrder workOrder : entry.getValue()){
				workOrder.setGuaranteeCode(guaranteeCode);
				guaranteeTask.setShopna(workOrder.getOrder().getShopna());
				guaranteeTask.setShopCode(workOrder.getOrder().getShopCode());
				workGuaranteeTaskDao.save(guaranteeTask);
				workOrderDao.save(workOrder);
			}
		}
	}
	
	/**
	 * 分页
	 * @param paramHolder
	 * @return
	 */
	public Page<WorkGuaranteeTask> loadWorkGuaranteeTask(QueryParamHolder paramHolder) {
		Page<WorkGuaranteeTask> tasks = findByJson(paramHolder, workGuaranteeTaskDao, WorkGuaranteeTask.class);
		return tasks;
	}
	
	
	public WorkGuaranteeTask findByTaskCode(String taskCode){
		return workGuaranteeTaskDao.findByTaskCode(taskCode);
	}
	
	@Transactional(readOnly=false)
	public void updateGuaranteeStatus(WorkGuaranteeTask task){
		List<WorkOrder> workOrders = workOrderDao.findByGuaranteeTaskCode(task.getTaskCode(),task.getBatchCode());
		for(WorkOrder order : workOrders){
			task.setGuaranteeStatus("1");
			if("".equals(order.getGuaranteeStatus())||order.getGuaranteeStatus()==null){
				task.setGuaranteeStatus("0");
				break;
			}
		}
		workGuaranteeTaskDao.save(task);
	}
}
